package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

@Api(tags = "店铺操作")
@RestController("adminShopStatusController")
@Slf4j
@RequestMapping("/admin/shop/")
public class ShopStatusController {

    private static final String KEY = "shop_status";

    @Autowired
    private RedisTemplate redisTemplate;

    @PutMapping("{status}")
    public Result<String> setShopStatus(@PathVariable Integer status){
        log.info("设置店铺状态为{}", status == 1 ? "营业" : "打样");
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(KEY, status.toString());
        return Result.success();
    }

    @GetMapping("status")
    public Result<Integer> getShopStatus(){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Integer status = Integer.parseInt((String) valueOperations.get(KEY));
        log.info("获取店铺状态为{}", status == 1 ? "营业" : "打样");
        return Result.success(status);
    }
}
